package com.example.ffepeg.ffeAdder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestAllVideo {
    public static void main(String[] args) {
        String ssmlStr = "<speak>欢迎来到英雄_联盟<break time='500ms'/>德玛西亚<break time='400ms'/>昂扬不灭<break time='500ms'/>正在待命</speak>";
       /* String cleaned = ssmlStr.replaceAll("[,\\.\\?!$#%&@*()\\-_+{}\\$\\$\\\\|/:;\"'/~`^]", "");
        System.out.println(cleaned);

        List<String> list = extractedPart(cleaned);
        System.out.println(list);

        for (String kj : list){
            Integer i = StringToTime(kj);
            System.err.println(i);
        }
*/
      /*  String breakTags = removeSpeakAndBreakTags(ssmlStr);
        System.out.println(breakTags);*/

      /*  String url = ""
        directoryCheck()*/


    }


    private static List<String> extractedPart(String ssmlString){
        List<String> extractedParts = new ArrayList<>();
        if (ssmlString.startsWith("<speak>") && ssmlString.endsWith("</speak>")) {
            ssmlString = ssmlString.substring(7, ssmlString.length() - 8);
        }
        String patternString = "(.*?)(<break time=\\d+ms>)";
        Pattern pattern = Pattern.compile(patternString, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(ssmlString);
        int lastEndIndex = 0;
        while (matcher.find()) {
            if (matcher.start() > lastEndIndex) {
                extractedParts.add(ssmlString.substring(lastEndIndex, matcher.start()).trim());
            }
            extractedParts.add(matcher.group().trim());
            lastEndIndex = matcher.end();
        }
        if (lastEndIndex < ssmlString.length()) {
            extractedParts.add(ssmlString.substring(lastEndIndex).trim());
        }
        return extractedParts;
    }



    private static Integer StringToTime(String text){
        Pattern pattern = Pattern.compile("(\\d+)ms");
        Matcher matcher = pattern.matcher(text);
        int number = 0;
        if (matcher.find()) {
            String numberStr = matcher.group(1);
            number = Integer.parseInt(numberStr);
            return number;
        }
        return number;
    }


    private static String removeSpeakAndBreakTags(String text) {
      /*  // 移除<speak>和</speak>标签
        String withoutSpeakTags = text.replaceAll("<speak>|</speak>", "");
        // 移除<break time='Xms'/>标签，其中X是任意数字
        String cleanText = withoutSpeakTags.replaceAll("<break time='\\d+ms'/>", "");*/
        //特殊符号
        String cleanTextSymbol = text.replaceAll("[,\\.\\?!$#%&@*()\\-_+{}\\$\\$\\\\|/:;\"'/~`^]", "");
        return cleanTextSymbol;
    }


    public static void directoryCheck(String dirPath){
        // 创建File对象
        File directory = new File(dirPath);
        // 检查目录是否存在
        if (directory.exists() && directory.isDirectory()) {
            System.out.println("目录已存在");
        } else {
            // 如果不存在，则尝试创建目录及其父目录
            boolean created = directory.mkdirs(); // mkdirs()会创建所有必要的父目录
            if (created) {
                System.out.println("目录已成功创建");
            } else {
                System.out.println("无法创建目录");
            }
        }
    }
}


